<?php
include_once 'header_php.php';
$title='Creation d\'association';
include_once 'header_html.php';

$nom=NULL;
$login=NULL;
$message="";


if(isset($_POST['envoi'])&&$_POST['envoi']=='ok'){
    
//le formulaire a �t� envoy�
    if(isset($_POST['nom'])&&strlen($_POST['nom'])>0){
        $nom=$_POST['nom'];    
    }
    else{
        $message="Vous devez entrer un nom <br/>";
    }
    if(isset($_POST['login'])&&strlen($_POST['login'])>0){
        $login=$_POST['login'];
    }        
    else{
        $message=$message."Vous devez entrer un login<br/>";
    }
        
    
    if(!isset($message)||strlen($message)==0){  
        
        date_default_timezone_set('Europe/Berlin');
        $date=date("Y");

        $sql='Select count(*) as nombrex From cotisation Where validee =1 AND annee=\''.$date.'\' AND personne=\''.$login.'\';';
   
        
        $query1= new DbQuery($db,$sql);
        $query1->SendQuery();
        $result=$query1->GetResult();
        
        
         if (!isset($result['nombrex'])||$result['nombrex']==0){
        //si la connexion n'a pas réussi, la fonction static renvoie FALSE
        
             $message="impossible d'ajouter asso";
        }else{
        date_default_timezone_set('Europe/Berlin');
        $date=date("Y-m-d");
        $sql='INSERT INTO Association(nom,datecreation,soldecompte,logo) VALUES(\''.$nom.'\',\''.$date.'\',0,\'/img/logo/default.jpg\');'; 
        $query2= new DbQuery($db,$sql);
        $query2->SendQuery();   
        
        
        $sql='INSERT INTO MembreAsso(semestre,personne,association,valide) VALUES(\''.getsemester().'\',\''.$login.'\',\''.$nom.'\',1);';
        $query3= new DbQuery($db,$sql);
        $query3->SendQuery();   
        
        
        $sql='SELECT id as idtemp FROM membreasso WHERE association=\''.$nom.'\' AND personne=\''.$login.'\' AND semestre=\''.GetSemester().'\';';    
        $query4= new DbQuery($db,$sql);
        $query4->SendQuery();  
        $result=$query4->GetResult();
        $idtemp=$result['idtemp'];
      
        
        $sql='INSERT INTO apourrole(membreasso, role)VALUES (\''.$idtemp.'\',\'Président\');';
        $query5= new DbQuery($db,$sql);
        $query5->SendQuery();    
        
        
        //echo '<meta http-equiv="Refresh" content="0;url=asso.php?nom='.$nom.'">';
        
        
        }
            
        }
}
        
        
    

?>
<div class="wrapper">
    <div class="container span8 offset2">
        <?php
            if(isset($_POST['envoi'])&&strlen($message)>0){
                echo '<div class="alert alert-danger">'.$message.'</div>';
           }

        ?>
        <form class="form-horizontal" action="<?php echo'ajouterasso.php' ?>" method="POST" accept-charset="UTF-8">
            <fieldset>
                <legend>Creer une nouvelle association</legend>
                <div class="control-group">
                    <div class="controls">
                        <label for="nom" class="control-label">Nom  </label>
                        <input  name="nom" id="nom" type="text" class="input-xlarge" placeholder="Nom asso"  <?php if(!is_null($nom)) echo 'value="'.$nom.'" ';?>/>
                    </div>
                </div>
                
                <div class="control-group">
                    <div class="controls">
                        <label for="login" class="control-label">Login du futur président  </label>
                        <input  name="login" id="login" type="text" class="input-xlarge" placeholder="login"  <?php if(!is_null($nom)) echo 'value="'.$login.'" ';?>/>
                    </div>
                </div>
                <div class="control-group">
                <input type="hidden" name="envoi" value="ok">
                <div class="form-actions">
                    <button type="submit" class="btn btn-primary">Creation</button>
                </div>
                </div>

            </fieldset>
        </form>          
    </div>

</div>



<?php include_once 'footer.php'; ?>

